//
//  HXDStatusTextLabel.swift
//  weibo
//
//  Created by huxuedong on 15/12/19.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

class HXDStatusTextLabel: UILabel {

    //特殊字符串
    var specialString: [HXDMatchResult]?
    //特殊字符串的选中遮罩视图数组
    private lazy var specialView: [UIView] = [UIView]()
    //特殊字符串的显示视图
    private lazy var textView: UITextView = {
        let textView = UITextView()
        //不允许编辑
        textView.editable = false
        textView.userInteractionEnabled = false
        textView.scrollEnabled = false
        textView.alpha = 0
        textView.textContainerInset = UIEdgeInsetsMake(0, -5, 0, -5)
        return textView
    }()
    override var attributedText: NSAttributedString? {
        didSet {
            textView.attributedText = attributedText
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        userInteractionEnabled = true
        addSubview(textView)
        textView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //获取手指触屏点的坐标
        let location = touches.first!.locationInView(self)
        //获取坐标位置字符在textView中的范围
        let textRange = textView.characterRangeAtPoint(location)
        //选中文字的范围
        textView.selectedTextRange = textRange
        //初始化textView中选中的范围，默认无选中(0,0)
        let range = textView.selectedRange
        if specialString == nil {
            return
        }
        //遍历所有特殊字符串
        for value in specialString! {
            //判断手指点击的坐标是否在特殊字符范围内
            if NSLocationInRange(range.location, value.range) {
                //将选中的范围设置为特殊字符串的范围
                textView.selectedRange = value.range
                //当textView中有选中的时候(selectedRange)，selectedTextRange更新为当前textView中选中的范围(当选中的内容出现折行时，则会有多个选中范围)
                let rects = textView.selectionRectsForRange(textView.selectedTextRange!)
                for value in rects {
                    let rect = value as! UITextSelectionRect
                    //创建特殊字符串遮罩视图
                    let cover = UIView(frame: rect.rect)
                    cover.layer.cornerRadius = 3
                    cover.layer.masksToBounds = true
                    cover.backgroundColor = RGB(red: 177, green: 215, blue: 255)
                    insertSubview(cover, atIndex: 0)
                    specialView.append(cover)
                }
            }
        }
    }
    
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        //取消触屏的时候移除特殊字符串交互视图
        for value in specialView {
            value.removeFromSuperview()
        }
        specialView.removeAll()
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touchesCancelled(touches, withEvent: event)
    }

}
